﻿using System;
using System.Collections;
using System.Xml;


namespace LogExceptions
{
    public class XmlConec
    {
        public static void UpdateAppSettings_(string KeyName, string KeyValue)
        {
            XmlDocument XmlDoc = new XmlDocument();
            XmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "XMLFiles\\" + "App.xml");
            foreach (XmlElement xElement in XmlDoc.DocumentElement)
            {
                if (xElement.Name == "appSettings")
                {
                    foreach (XmlNode xNode in xElement.ChildNodes)
                    {
                        if (xNode.Attributes[0].Value == KeyName)
                            xNode.Attributes[1].Value = KeyValue;
                    }
                }
            }
            XmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "XMLFiles\\" + "App.xml");
        }

        public static string getAppSettings_(string KeyName)
        {
            string Keyvalue = "";
            XmlDocument XmlDoc = new XmlDocument();
            XmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "XMLFiles\\" + "App.xml");
            foreach (XmlElement xElement in XmlDoc.DocumentElement)
            {
                if (xElement.Name == "appSettings")
                {
                    foreach (XmlNode xNode in xElement.ChildNodes)
                    {
                        if (xNode.Attributes[0].Value == KeyName)
                            Keyvalue = xNode.Attributes[1].Value.ToString();
                    }
                }
            }
            return Keyvalue;
        }

        public static void UpdatePrintSettings_(string KeyName, string KeyValue)
        {
            XmlDocument XmlDoc = new XmlDocument();
            XmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "XMLFiles\\" + XmlConec.getAppSettings_("PrintXMLFile"));
            foreach (XmlElement xElement in XmlDoc.DocumentElement)
            {
                if (xElement.Name == "prnSettings")
                {
                    foreach (XmlNode xNode in xElement.ChildNodes)
                    {
                        if (xNode.Attributes[0].Value == KeyName)
                            xNode.Attributes[1].Value = KeyValue;
                    }
                }
            }
            XmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory + "XMLFiles\\" + XmlConec.getAppSettings_("PrintXMLFile"));
        }

        public static int getPrintSettings_(string KeyName)
        {
            string Keyvalue = "";
            XmlDocument XmlDoc = new XmlDocument();
            XmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "XMLFiles\\" + XmlConec.getAppSettings_("PrintXMLFile"));
            foreach (XmlElement xElement in XmlDoc.DocumentElement)
            {
                if (xElement.Name == "prnSettings")
                {
                    foreach (XmlNode xNode in xElement.ChildNodes)
                    {
                        if (xNode.Attributes[0].Value == KeyName)
                            Keyvalue = xNode.Attributes[1].Value.ToString();
                    }
                }
            }
            return int.Parse(Keyvalue);
        }

        public static int getReportSettings_(string KeyName, string strXmlFile)
        {
            string Keyvalue = "";
            XmlDocument XmlDoc = new XmlDocument();
            XmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "XMLFiles\\" + strXmlFile);
            foreach (XmlElement xElement in XmlDoc.DocumentElement)
            {
                if (xElement.Name == "prnSettings")
                {
                    foreach (XmlNode xNode in xElement.ChildNodes)
                    {
                        if (xNode.Attributes[0].Value == KeyName)
                            Keyvalue = xNode.Attributes[1].Value.ToString();
                    }
                }
            }
            return int.Parse(Keyvalue);
        }

        public static string getPrinter()
        {
            string Keyvalue = "";
            XmlDocument XmlDoc = new XmlDocument();
            XmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "XMLFiles\\" + XmlConec.getAppSettings_("PrintXMLFile"));
            foreach (XmlElement xElement in XmlDoc.DocumentElement)
            {
                if (xElement.Name == "prnSettings")
                {
                    foreach (XmlNode xNode in xElement.ChildNodes)
                    {
                        if (xNode.Attributes[0].Value == "Printer")
                            Keyvalue = xNode.Attributes[1].Value.ToString();
                    }
                }
            }
            return Keyvalue;
        }

        public static ArrayList getPrintNodes_()
        {
            ArrayList arlNodes = new ArrayList();
            XmlDocument XmlDoc = new XmlDocument();
            XmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "XMLFiles\\" + XmlConec.getAppSettings_("PrintXMLFile"));
            foreach (XmlElement xElement in XmlDoc.DocumentElement)
            {
                if (xElement.Name == "prnSettings")
                {
                    foreach (XmlNode xNode in xElement.ChildNodes)
                        arlNodes.Add(xNode.Attributes[0].Value);
                }
            }
            return arlNodes;
        }

        public static string GetSQLConection()
        {
            return GetSQLConection(false);
        }

        public static string GetSQLConection(bool bSourceServer)
        {
            return GetSQLConection(bSourceServer, null);
        }

        public static string GetSQLConection(string strNewDB)
        {
            return GetSQLConection(false, strNewDB);
        }

        public static string GetSQLConection(bool bSourceServer, string strNewDB)
        {
            string strConec = "", strServer = getAppSettings_("SQLServer"), strDatabase = getAppSettings_("SQLDatabase");
            string strSQLUserId = getAppSettings_("SQLUserId"), strSQLPassword = getAppSettings_("SQLPassword");

            if (bSourceServer == true)
                strServer = getAppSettings_("SourceServer");
            if (strNewDB != null)
                strDatabase = strNewDB;

            if (getAppSettings_("Authentic") == "false")
                strConec = "Data Source=" + strServer + ";Initial Catalog=" + strDatabase + ";Integrated Security=true";
            else
            {
                if (strSQLPassword != null)
                    strConec = "Data Source=" + strServer + ";Initial Catalog=" + strDatabase + ";User ID=" + strSQLUserId + ";Password=" + strSQLPassword;
                else
                    strConec = "Data Source=" + strServer + ";Initial Catalog=" + strDatabase + ";User ID=" + strSQLUserId;
            }
            strConec = strConec + ";Pooling=false;Connection Timeout=200";
            return strConec;
        }
    }
}
